package com.qianji.controller;

import cn.hutool.json.JSONObject;
import com.qianji.pojo.ActiveDto;
import com.qianji.service.ActiveService;
import com.qianji.utils.Result;
import com.qianji.vo.ActiveAddVO;
import com.qianji.vo.ActiveUpdateVO;
import com.qianji.vo.Introduction;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.alibaba.excel.util.StringUtils.isEmpty;

/**
 * Created by 阳家桂 on 2021/3/12 15:41
 */
@RestController
@Slf4j
@ApiModel("活动模块")
@RequestMapping("/active/")
public class ActiveController {

    @Autowired
    private ActiveService activeService;

    /**
     * 新增活动
     * @param activeAddVO
     * @return
     */
    @ApiOperation("新增")
    @RequestMapping(value = "activeAdd",method = RequestMethod.POST)
    public Result addActive(@RequestBody ActiveAddVO activeAddVO){
        if(isEmpty(activeAddVO)){
            return Result.failed("请填写要创建的活动信息",400);
        }
       Integer result = activeService.addActive(activeAddVO);
       if (result>0){
          return Result.success("添加成功",200);
       }else {
         return  Result.failed("添加失败",400);
       }
    }


    /**
     * 根据id删除活动
     * @param id
     * @return
     */
    @ApiOperation("根据id删除活动")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Result deletActiveById(String id){
        if(null==id){
            return Result.failed("请问您要删除哪条活动信息？",400);
        }
        Integer result = activeService.deletActiveById(id);
        if (result>0){
            return Result.success("删除成功",200);
        }else {
            return  Result.failed("删除失败",400);
        }
    }

    /**
     * 修改活动数据
     * @param activeUpdateVO
     * @return
     */
    @ApiOperation("修改")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public Result updateActive(@RequestBody ActiveUpdateVO activeUpdateVO){
        if(isEmpty(activeUpdateVO)){
            return Result.failed("请填写要修改的信息",400);
        }

        Integer result = activeService.updateActive(activeUpdateVO);
        if (result>0){
            return Result.success("修改成功！",200);
        }else {
            return Result.failed("修改失败",400);
        }
    }


    /**
     * 根据传入参数查询对应的活动
     * @param activeDto
     * @return
     */
    @ApiOperation("查询")
    @RequestMapping(value = "select",method = RequestMethod.POST)
    public Result selectAll(@RequestBody ActiveDto activeDto){

        return activeService.selectAll(activeDto);
    }

    /**
     * 关闭活动
     * @param id
     * @return
     */
    @ApiOperation("关闭或开启活动")
    @RequestMapping(value = "onoff",method = RequestMethod.POST)
    public Result openAndCloseActiveById( String id){
        Integer result = activeService.onOff(id);
        if (result>0){
            return Result.success("操作成功",200);
        }else {
            return  Result.failed("操作失败",400);
        }
    }

    /**
     * 获取红包类型活动数据接口
     * @param introduction
     * @return
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "findById",method = RequestMethod.POST)
    public Result<ActiveDto> selectById(@RequestBody Introduction introduction) {
        ActiveDto active = activeService.selectById(introduction.getData().getId());

        return Result.success(active);
    }

    /**
     * 获取砸蛋活动数据接口
     * @param introduction
     * @return
     */
    @ApiOperation("砸蛋活动")
    @RequestMapping(value = "smashEggs")
    public Result<ActiveDto> smashEggs(@RequestBody Introduction introduction) {

        ActiveDto active = activeService.smashEggs(introduction.getData().getId());

        return Result.success(active);
    }

    @ApiOperation("活动")
    @RequestMapping(value = "turntable",method = RequestMethod.POST)
    public Result turntable(@RequestBody Introduction introduction){

        String turntableData = activeService.findTurntable(introduction.getData().getId());
        JSONObject jsonObject = new JSONObject(turntableData);

        return Result.success(jsonObject);
    }

}
